/*----------------------------------------------------*/
/* Import data and initialize log file */
/*----------------------------------------------------*/

clear all  // Clear existing variables and datasets

log using "$logs\t-test.log", replace  // Start logging output

use "$input\ifp_scrapped_aggregated_industries_modified.dta", clear  // Load dataset

/*----------------------------------------------------*/
/* Prepare credit line data */
/*----------------------------------------------------*/

preserve
    keep if delta < 0  // Keep only firms before new loan event
    collapse (sum) co, by(siren)  // Aggregate credit line variable by firm ID
    gen codummy = co > 0  // Create a dummy variable indicating credit line use
    drop co  // Drop original credit line variable
    save "$output\data_co_dummy.dta", replace  // Save processed dataset
restore

/* Convert rating categories into numerical groups */
egen ratingint = group(rating)

/*----------------------------------------------------*/
/* PART I.3: Summary Statistics Preparation */
/*----------------------------------------------------*/

keep if delta == -11  // Keep observations from 11 months before the new outside loan

/* Define labels for firm type and time period */
label define p2p 1 "FinTech" 0 "Bank"
label values p2p p2p
label define post 1 "After" 0 "Before"
label values post post

/* Create firm type indicators */
gen firm_type = p2p  // 1 for FinTech firms, 0 for Bank firms
gen type_reverse = 1 - firm_type  // Reverse coding for comparison

/* Adjust rating categories */
replace ratingint = ratingint - 1  // Shift rating index down by 1
replace ratingint = 13 if ratingint == 0  // Assign missing ratings to category 13

/* Save dataset containing only FinTech firms */
preserve
    keep if p2p == 1
    save "$output\ifp_scrapped_aggregated_industries_modified_static.dta", replace
restore

/* Save firm list for reference */
preserve
    save "$output\ifp_ttest_siren_list.dta", replace
restore

/*----------------------------------------------------*/
/* Table 3: Comparing Firm Characteristics */
/*----------------------------------------------------*/

/* Merge dataset with credit line dummy */
merge 1:1 siren using "$output\data_co_dummy.dta"
keep if _merge == 3  // Keep only matched firms
drop _merge

/* Label firm characteristic variables */
label var ageatevent "Age"
label var r1filled "Working capital"
label var r3filled "EBIT"
label var r5filled "Sales"
label var r12filled "Investment"
label var r13filled "Debt"
label var logassets_filled "Total Assets"
label var logemployees "Employment"
label var r_collateral_filled "Tangible assets"
label var rdcostsfilled "R&D costs filled"
label var ratingint "Rating"
label var outsideloan "New relationship loan"
label var codummy "$\mathbbm{1}$(Credit line)"

/* Define list of firm characteristics for comparison */
global firm_var `"ratingint r_collateral_filled logemployees r13filled r12filled r3filled r1filled ageatevent logassets_filled outsideloan codummy"'

/*----------------------------------------------------*/
/* Compute Summary Statistics */
/*----------------------------------------------------*/

/* Generate summary tables comparing FinTech and Bank borrowers */
eststo bank: estpost tabstat $firm_var if firm_type == 0, s(mean) c(statistics)
eststo bank_p2p: estpost tabstat $firm_var if firm_type == 1, s(mean) c(statistics)
eststo t_test01: estpost ttest $firm_var, by(type_reverse)
eststo sum0: estpost sum $firm_var if firm_type == 0
eststo sum1: estpost sum $firm_var if firm_type == 1

/* Format and save Table 3 */
esttab bank_p2p bank t_test01 t_test01 sum1 sum0 using "$tables\Table_3a_Comparing_FinTech_and_Bank_Borrowers_candidates_full.tex", ///
stats(N, label(`"N"') layout("") fmt(%9.0fc) ) star(* 0.10 ** 0.05 *** 0.01) ///
replace nonote nogaps booktabs fragment label noobs nolines nomtitles ///
cells("mean(fmt(%9.3f) pattern(1 1 0 0 0 0)) b(fmt(%9.3f) pattern(0 0 1 0 0 0)) t(fmt(%9.3f) pattern(0 0 0 1 0 0) star) count(fmt(%9.0fc) pattern(0 0 0 0 1 1))") ///
mgroups("(a) FinTech" "(b) Bank" "(a)-(b)" "\emph{t}-statistic" "(a) Count" "(b) Count", pattern(1 1 1 1 1 1) ///
prefix(\multicolumn{@span}{c}{) suffix(}) span ) collabels(none) ///
posthead(\midrule)

/*----------------------------------------------------*/
/* Trim outliers using winsorization */
/*----------------------------------------------------*/

foreach x of varlist $firm_var {
    winsor2 `x', suffix(_trim) cut(1 99) trim
}

/* Define list of trimmed firm characteristics */
global firm_var_trim `"logassets_filled_trim ageatevent_trim r1filled_trim r3filled_trim r12filled_trim r13filled_trim logemployees_trim r_collateral_filled_trim ratingint_trim outsideloan_trim codummy_trim"'

/*----------------------------------------------------*/
/* PSM Outcome Processing */
/*----------------------------------------------------*/

/* Load matched sample dataset */
use "$output\matched_sample_nn.dta", clear

/* Process credit line data */
preserve
    keep if delta < 0
    collapse (sum) co, by(siren)
    gen codummy = co > 0
    drop co
    save "$output\data_psm_co_dummy.dta", replace
restore

/* Keep observations from 11 months before the new loan */
keep if delta == -11

/* Define labels for firm type and time period */
label define p2p 1 "FinTech" 0 "Bank"
label values p2p p2p
label define post 1 "After" 0 "Before"
label values post post

/* Generate firm type indicators */
gen firm_type = p2p
gen type_reverse = 1 - firm_type
egen ratingint = group(rating)

/* Adjust rating categories */
replace ratingint = ratingint - 1
replace ratingint = 13 if ratingint == 0

/* Define firm characteristics */
global firm_var `"ratingint r_collateral_filled logemployees r13filled r12filled r3filled r1filled ageatevent logassets_filled outsideloan codummy"'

/*----------------------------------------------------*/
/* Post-PSM Summary Statistics */
/*----------------------------------------------------*/

preserve
    /* Merge with processed credit line dataset */
    merge 1:1 siren using "$output\data_psm_co_dummy.dta"
    keep if _merge == 3
    drop _merge

    /* Assign labels for firm characteristics */
    label var ageatevent "Age"
    label var r1filled "Working capital"
    label var r3filled "EBIT"
    label var r5filled "Sales"
    label var r12filled "Investment"
    label var r13filled "Debt"
    label var logassets_filled "Total Assets"
    label var logemployees "Employment"
    label var r_collateral_filled "Tangible assets"
    label var rdcostsfilled "R&D costs filled"
    label var ratingint "Rating"
    label var outsideloan "New relationship loan"
    label var codummy "$\mathbbm{1}$(Credit line)"

    /* Expand sample using matching weights */
    su weight, d
    gen max_weight = `r(max)'
    expand max_weight
    bys siren mdate: gen nb = _n
    keep if nb <= weight
    drop nb

    /* Generate post-PSM summary statistics */
    eststo bank: estpost tabstat $firm_var if firm_type == 0, s(mean) c(statistics)
    eststo bank_p2p: estpost tabstat $firm_var if firm_type == 1, s(mean) c(statistics)
    eststo t_test01: estpost ttest $firm_var, by(type_reverse)

    /* Save results */
    esttab bank_p2p bank t_test01 using "$tables\Table_3b_Comparing_FinTech_and_Bank_Borrowers_candidates_post_PSM_full.tex", ///
stats(N, label(`"N"') layout("") fmt(%9.0fc) ) star(* 0.10 ** 0.05 *** 0.01) ///
replace nonote nogaps booktabs fragment label noobs nolines nomtitles ///
cells("mean(fmt(%9.3f) pattern(1 1 0 0 0 0)) b(fmt(%9.3f) pattern(0 0 1 0 0 0)) t(fmt(%9.3f) pattern(0 0 0 1 0 0) star) count(fmt(%9.0fc) pattern(0 0 0 0 1 1))") ///
mgroups("(a) FinTech" "(b) Bank" "(a)-(b)" "\emph{t}-statistic" "(a) Count" "(b) Count", pattern(1 1 1 1 1 1) ///
prefix(\multicolumn{@span}{c}{) suffix(}) span ) collabels(none) ///
posthead(\midrule)

    save "$output\check_sample_new.dta", replace
	restore

//------------------------------------------------------------------------------------//
//                      T-Test Visuals - Matched vs. Unmatched                        //
//------------------------------------------------------------------------------------//

/* Define the list of firm characteristics to be analyzed */
local variables `"logassets_filled ageatevent r1filled r3filled r12filled r13filled logemployees r_collateral_filled ratingint outsideloan codummy"'

/* Define treatment variable: 1 for FinTech firms, 0 for Bank firms */
gen treated = p2p == 1

/* Merge dataset with previously created credit line dummy dataset */
merge 1:1 siren using "$output\data_psm_co_dummy.dta"
keep if _merge == 3  // Keep only matched firms
drop _merge

/*------------------------------------------------------------------------------------*/
/*            Loop through variables to compute standardized coefficients            */
/*------------------------------------------------------------------------------------*/

local i = 0  // Initialize counter
foreach var of local variables {
    local i = `i' + 1  // Increment counter
    preserve
        /* Standardize variable */
        egen `var'_std = std(`var')

        /* Perform weighted least squares regression */
        eststo ols_`var' : reg `var'_std treated [pw = weight]

        /* Store regression coefficients and standard errors */
        gen rank = `i' 
        gen B_wls = _b[treated]  // Coefficient for treated group
        gen S_wls = _se[treated]  // Standard error

        /* Keep only the first row for each variable */
        keep rank B_wls S_wls
        keep if _n == 1

        /* Save results */
        save "$output\output_wls_`i'.dta", replace
    restore
}

/*------------------------------------------------------------------------------------*/
/*                      Merge and Combine Results for Visualization                  */
/*------------------------------------------------------------------------------------*/

local max = `i'  // Store max index

/* Save and append OLS results */
clear
save "$output\output_ols", replace emptyok
forval i = 1/`max' {
    sleep 1000  // Introduce delay to avoid conflicts
    append using "$output\output_ols_`i'"
}
save "$output\output_ols", replace

/* Save and append Weighted Least Squares (WLS) results */
clear
save "$output\output_wls", replace emptyok
forval i = 1/`max' {
    sleep 1000
    append using "$output\output_wls_`i'"
}
save "$output\output_wls", replace

/* Load and merge results */
clear 
use "$output\output_ols"
merge 1:1 rank using "$output\output_wls"

/*------------------------------------------------------------------------------------*/
/*                      Compute Confidence Intervals for Plots                        */
/*------------------------------------------------------------------------------------*/

/* Compute confidence intervals (95%) */
gen low_ols = B_ols - 1.96 * S_ols
gen high_ols = B_ols + 1.96 * S_ols
gen low_wls = B_wls - 1.96 * S_wls
gen high_wls = B_wls + 1.96 * S_wls

/* Adjust position of labels in graph */
gen rank_ols = rank + 0.05
gen rank_wls = rank - 0.05

/*------------------------------------------------------------------------------------*/
/*                           Generate T-Test Visualization                            */
/*------------------------------------------------------------------------------------*/

twoway 	(rcap high_ols low_ols rank_ols, lwidth(1) msize(0)   lcolor(emerald) horizontal)  /*
        */ (rcap high_wls low_wls rank_wls, lwidth(1)   msize(0)   lcolor(gs10%40) horizontal) /*
        */ (scatter rank_ols B_ols, mfcolor(white)  msize(1.8)  mcolor(emerald)) /*
        */ (scatter rank_wls B_wls, mfcolor(white)   msize(1.8)  msymbol(triangle) mcolor(gs10%40)) ,/*
        */ graphregion(fcolor(white)) xline(0) /*
        */ legend(order(3 "Unmatched sample" 4 "Matched sample") cols(2) ) ylabel(,nogrid) /*
        */ ylabel(1 "Total Assets" 2 "Age" 3 "Working capital" 4 "EBIT" 5 "Investment" 6 "Debt" 7 "Employment" 8 "Tangible assets" 9 "Rating"  10 "New relationship loan" 11 "1(Credit line)" , angle(horizontal))

/* Export visualization as a PDF */
graph export "$figures/Figure_2a_ifp_t_test_plot.pdf", replace

/* Close log file */
log close
